//
//  UIAlterView+ProgressView.m
//  downFileTest
//
//  Created by 拼凑的断音 on 16/6/22.
//  Copyright © 2016年 拼凑的断音. All rights reserved.
//

#import "UIAlterView+ProgressView.h"
#import "XXTool.h"

@interface UIAlertView ()

@property UIView *progressContent;

@property UIProgressView *progress;

@property UILabel *progressText;

@end

@implementation  UIAlertView(ProgressView)

#pragma mark- 控件的动态绑定
static char XXProgressContentKey;
static char XXAlterWithProgressKey;
static char XXAlterProgressLabelKey;

-(UIView*)progressContent
{
    return objc_getAssociatedObject(self, &XXProgressContentKey);
}

-(void)setProgressContent:(UIView *)progressContent
{
    [self willChangeValueForKey:@"XXProgressContentKey"];
    objc_setAssociatedObject(self
                             , &XXProgressContentKey
                             , progressContent
                             , OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self didChangeValueForKey:@"XXProgressContentKey"];
}


-(UIProgressView*)progress
{
    return objc_getAssociatedObject(self, &XXAlterWithProgressKey);
}

-(void)setProgress:(UIProgressView *)progress
{
    [self willChangeValueForKey:@"XXAlterWithProgressKey"];
    objc_setAssociatedObject(self
                             , &XXAlterWithProgressKey
                             , progress
                             , OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self didChangeValueForKey:@"XXAlterWithProgressKey"];
}

-(UILabel*)progressText
{
    return objc_getAssociatedObject(self, &XXAlterProgressLabelKey);
}

-(void)setProgressText:(UILabel *)progressText
{
    [self willChangeValueForKey:@"XXAlterProgressLabelKey"];
    objc_setAssociatedObject(self
                             , &XXAlterProgressLabelKey
                             , progressText
                             , OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self didChangeValueForKey:@"XXAlterProgressLabelKey"];
}

-(void)showWithProgress
{
    self.progressContent = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200
                                                                    , 30)];
    self.progressContent.backgroundColor = [UIColor clearColor];
    
    
    self.progress = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
    self.progress.frame = CGRectMake(30, 15, 180, 15);
    self.progress.progressTintColor = [XXTool colorWithProject];
    //self.progress.progress = 0.5;
    [self.progressContent addSubview:self.progress];
    
    self.progressText = [[UILabel alloc] initWithFrame:CGRectMake(self.progress.frame.size.width+self.progress.frame.origin.x+10, 0, 40, self.progressContent.frame.size.height)];
    self.progressText.font = [UIFont systemFontOfSize:14];
    self.progressText.text = [NSString stringWithFormat:@"%.0f%%",self.progress.progress*100];
    [self.progressContent addSubview:self.progressText];
    
    if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1)
    {
        [self setValue:self.progressContent forKey:@"accessoryView"];
    }
    else
    {
        [self addSubview:self.progressContent];
    }
}

-(void)setProgressWithRate:(float)rate
{
    [self.progress setProgress:rate animated:NO];
    self.progressText.text = [NSString stringWithFormat:@"%.0f%%",rate*100];
}

@end
